﻿Imports System.Collections.Generic
Imports System.Web
Imports System.Xml
Imports DotNetNuke.Services.Journal

Namespace DotNetNuke.Modules.ActiveForums
#Region "TopicInfo Class"
    Public Class TopicInfo
        Public Sub New()
            Content = New Content
            Security = New PermissionInfo
            Author = New Author
        End Sub
#Region "Private Members"
        Private _TopicId As Integer
        Private _ContentId As Integer
        Private _ViewCount As Integer
        Private _ReplyCount As Integer
        Private _IsLocked As Boolean
        Private _IsPinned As Boolean
        Private _TopicIcon As String
        Private _StatusId As Integer
        Private _IsApproved As Boolean
        Private _IsDeleted As Boolean
        Private _IsAnnounce As Boolean
        Private _IsArchived As Boolean
        Private _TopicType As TopicTypes
        Private _AnnounceStart As Date
        Private _AnnounceEnd As Date
        Private _Content As Content
        Private _PermissionInfo As PermissionInfo
        Private _author As Author
        Private _tags As String
#End Region
#Region "Public Properties"
        Public Property TopicId() As Integer
            Get
                Return _TopicId
            End Get
            Set(ByVal value As Integer)
                _TopicId = value
            End Set
        End Property
        Public Property ContentId() As Integer
            Get
                Return _ContentId
            End Get
            Set(ByVal value As Integer)
                _ContentId = value
            End Set
        End Property
        Public Property ViewCount() As Integer
            Get
                Return _ViewCount
            End Get
            Set(ByVal value As Integer)
                _ViewCount = value
            End Set
        End Property
        Public Property ReplyCount() As Integer
            Get
                Return _ReplyCount
            End Get
            Set(ByVal value As Integer)
                _ReplyCount = value
            End Set
        End Property
        Public Property IsLocked() As Boolean
            Get
                Return _IsLocked
            End Get
            Set(ByVal value As Boolean)
                _IsLocked = value
            End Set
        End Property
        Public Property IsPinned() As Boolean
            Get
                Return _IsPinned
            End Get
            Set(ByVal value As Boolean)
                _IsPinned = value
            End Set
        End Property
        Public Property TopicIcon() As String
            Get
                Return _TopicIcon
            End Get
            Set(ByVal value As String)
                _TopicIcon = value
            End Set
        End Property
        Public Property StatusId() As Integer
            Get
                Return _StatusId
            End Get
            Set(ByVal value As Integer)
                _StatusId = value
            End Set
        End Property
        Public Property IsApproved() As Boolean
            Get
                Return _IsApproved
            End Get
            Set(ByVal value As Boolean)
                _IsApproved = value
            End Set
        End Property
        Public Property IsDeleted() As Boolean
            Get
                Return _IsDeleted
            End Get
            Set(ByVal value As Boolean)
                _IsDeleted = value
            End Set
        End Property
        Public Property IsAnnounce() As Boolean
            Get
                Return _IsAnnounce
            End Get
            Set(ByVal value As Boolean)
                _IsAnnounce = value
            End Set
        End Property
        Public Property IsArchived() As Boolean
            Get
                Return _IsArchived
            End Get
            Set(ByVal value As Boolean)
                _IsArchived = value
            End Set
        End Property
        Public Property TopicType() As TopicTypes
            Get
                Return _TopicType
            End Get
            Set(ByVal value As TopicTypes)
                _TopicType = value
            End Set
        End Property
        Public Property AnnounceStart() As Date
            Get
                Return _AnnounceStart
            End Get
            Set(ByVal value As Date)
                _AnnounceStart = value
            End Set
        End Property
        Public Property AnnounceEnd() As Date
            Get
                Return _AnnounceEnd
            End Get
            Set(ByVal value As Date)
                _AnnounceEnd = value
            End Set
        End Property
        Public Property Content() As Content
            Get
                Return _Content
            End Get
            Set(ByVal value As Content)
                _Content = value
            End Set
        End Property
        Public Property Security() As PermissionInfo
            Get
                Return _PermissionInfo
            End Get
            Set(ByVal value As PermissionInfo)
                _PermissionInfo = value
            End Set
        End Property
        Public Property Author() As Author
            Get
                Return _author
            End Get
            Set(ByVal value As Author)
                _author = value
            End Set
        End Property
        Public Property Tags() As String
            Get
                Return _tags
            End Get
            Set(ByVal value As String)
                _tags = value
            End Set
        End Property
        Public Property Priority() As Integer = 0
        Public Property Categories() As String = String.Empty
        Public Property TopicUrl() As String = String.Empty
        Public Property ForumURL() As String = String.Empty
        Public Property TopicData() As String = String.Empty
        Public ReadOnly Property URL() As String
            Get
                If Not String.IsNullOrEmpty(TopicUrl) And Not String.IsNullOrEmpty(ForumURL) Then
                    If TopicUrl.StartsWith("/") And ForumURL.EndsWith("/") Then
                        ForumURL = ForumURL.Substring(0, ForumURL.Length - 1)
                        If Not ForumURL.StartsWith("/") Then
                            ForumURL = "/" & ForumURL
                        End If
                    End If
                    Return ForumURL & TopicUrl
                Else
                    Return String.Empty
                End If
            End Get
        End Property
        Public ReadOnly Property TopicProperties() As List(Of PropertiesInfo)
            Get
                If TopicData = String.Empty Then
                    Return Nothing
                Else
                    Dim pl As New List(Of PropertiesInfo)
                    Dim xDoc As New XmlDocument
                    xDoc.LoadXml(TopicData)
                    If Not xDoc Is Nothing Then
                        Dim xRoot As System.Xml.XmlNode = xDoc.DocumentElement
                        Dim xNodeList As System.Xml.XmlNodeList = xRoot.SelectNodes("//properties/property")
                        If xNodeList.Count > 0 Then
                            Dim i As Integer = 0
                            For i = 0 To xNodeList.Count - 1
                                Dim pName As String = Utilities.HTMLDecode(xNodeList(i).ChildNodes.Item(0).InnerText)
                                Dim pValue As String = Utilities.HTMLDecode(xNodeList(i).ChildNodes.Item(1).InnerText)
                                Dim pId As Integer = CInt(xNodeList(i).Attributes("id").Value)
                                Dim p As New PropertiesInfo
                                p.Name = pName
                                p.DefaultValue = pValue
                                p.PropertyId = pId
                                pl.Add(p)
                            Next
                        End If
                    End If
                    Return pl
                End If
            End Get
        End Property

#End Region
    End Class
#End Region
#Region "Topics Controller"
    Public Class TopicsController
        Implements DotNetNuke.Entities.Modules.ISearchable
        Public Function Topic_QuickCreate(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal ForumId As Integer, ByVal Subject As String, ByVal Body As String, ByVal UserId As Integer, ByVal DisplayName As String, ByVal IsApproved As Boolean, ByVal IPAddress As String) As Integer
            Dim topicId As Integer = -1
            Dim ti As New TopicInfo
            With ti
                .AnnounceEnd = Utilities.NullDate
                .AnnounceStart = Utilities.NullDate
                .Content.AuthorId = UserId
                .Content.AuthorName = DisplayName
                .Content.Subject = Subject
                .Content.Body = Body
                .Content.Summary = String.Empty

                .Content.IPAddress = IPAddress
                Dim dt As Date = Now
                .Content.DateCreated = dt
                .Content.DateUpdated = dt
                .IsAnnounce = False
                .IsApproved = IsApproved
                .IsArchived = False
                .IsDeleted = False
                .IsLocked = False
                .IsPinned = False
                .ReplyCount = 0
                .StatusId = -1
                .TopicIcon = String.Empty
                .TopicType = TopicTypes.Topic
                .ViewCount = 0
            End With
            topicId = TopicSave(PortalId, ti)
            If topicId > 0 Then
                Topics_SaveToForum(ForumId, topicId, PortalId, ModuleId)
                If UserId > 0 Then
                    Dim uc As New Data.Profiles
                    uc.Profile_UpdateTopicCount(PortalId, UserId)
                End If
            End If
            Return topicId
        End Function
        Public Function TopicSave(ByVal PortalId As Integer, ByVal ti As TopicInfo) As Integer
            Return CType(DataProvider.Instance().Topics_Save(PortalId, ti.TopicId, ti.ViewCount, ti.ReplyCount, ti.IsLocked, ti.IsPinned, ti.TopicIcon, ti.StatusId, ti.IsApproved, ti.IsDeleted, ti.IsAnnounce, ti.IsArchived, ti.AnnounceStart, ti.AnnounceEnd, ti.Content.Subject.Trim, ti.Content.Body.Trim, ti.Content.Summary.Trim, ti.Content.DateCreated, ti.Content.DateUpdated, ti.Content.AuthorId, ti.Content.AuthorName, ti.Content.IPAddress, ti.TopicType, ti.Priority, ti.TopicUrl, ti.TopicData), Integer)
        End Function
        Public Function Topics_SaveToForum(ByVal ForumId As Integer, ByVal TopicId As Integer, ByVal PortalId As Integer, ByVal ModuleId As Integer) As Integer
            Dim id As Integer = Topics_SaveToForum(ForumId, TopicId, PortalId, ModuleId, -1)
            Return id
        End Function
        Public Function Topics_SaveToForum(ByVal ForumId As Integer, ByVal TopicId As Integer, ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal LastReplyId As Integer) As Integer
            Dim id As Integer = CType(DataProvider.Instance().Topics_SaveToForum(ForumId, TopicId, LastReplyId), Integer)

            Return id
        End Function
        Public Function TopicsList(ByVal ForumId As Integer, ByVal ModuleId As Integer, ByVal PortalId As Integer) As List(Of TopicInfo)
            Dim dr As IDataReader = DataProvider.Instance().Topics_List(ForumId, ModuleId, PortalId)
            Dim tl As New List(Of TopicInfo)
            While dr.Read()
                Dim ti As New TopicInfo
                With ti
                    If Not dr("AnnounceEnd") Is DBNull.Value Then
                        .AnnounceEnd = CDate(dr("AnnounceEnd"))
                    End If

                    If Not dr("AnnounceStart") Is DBNull.Value Then
                        .AnnounceStart = CDate(dr("AnnounceStart"))
                    End If
                    .Content.AuthorId = CInt(dr("AuthorId"))
                    .Content.AuthorName = dr("AuthorName").ToString
                    .Content.Body = dr("Body").ToString
                    .Content.ContentId = CInt(dr("ContentId"))
                    .Content.DateCreated = CDate(dr("DateCreated"))
                    .Content.DateUpdated = CDate(dr("DateUpdated"))
                    .Content.IsDeleted = CBool(dr("IsDeleted"))
                    .Content.Subject = dr("Subject").ToString
                    .Content.Summary = dr("Summary").ToString
                    .Author.AuthorId = .Content.AuthorId
                    .Author.DisplayName = dr("DisplayName").ToString
                    .Author.Email = dr("Email").ToString
                    .Author.FirstName = dr("FirstName").ToString
                    .Author.LastName = dr("LastName").ToString
                    .Author.Username = dr("Username").ToString
                    .ContentId = CInt(dr("ContentId"))
                    .IsAnnounce = CBool(dr("IsAnnounce"))
                    .IsApproved = CBool(dr("IsApproved"))
                    .IsArchived = CBool(dr("IsArchived"))
                    .IsDeleted = CBool(dr("IsDeleted"))
                    .IsLocked = CBool(dr("IsLocked"))
                    .IsPinned = CBool(dr("IsPinned"))
                    .ReplyCount = CInt(dr("ReplyCount"))
                    .StatusId = CInt(dr("StatusId"))
                    .TopicIcon = CStr(dr("TopicIcon"))
                    .TopicId = CInt(dr("TopicId"))
                    .TopicType = CType(dr("TopicType"), TopicTypes)
                    .ViewCount = CInt(dr("ViewCount"))
                    .Tags = dr("Tags").ToString
                End With
                tl.Add(ti)
            End While
            dr.Close()
            Return tl
        End Function
        Public Function Topics_Get(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal TopicId As Integer) As TopicInfo
            Return Topics_Get(PortalId, ModuleId, TopicId, -1, -1, False)
        End Function
        Public Function Topics_Get(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal TopicId As Integer, ByVal ForumId As Integer, ByVal UserId As Integer, ByVal WithSecurity As Boolean) As TopicInfo
            Dim dr As IDataReader = DataProvider.Instance().Topics_Get(PortalId, ModuleId, TopicId, ForumId, UserId, WithSecurity)
            Dim ti As TopicInfo = Nothing
            While dr.Read()
                ti = New TopicInfo
                With ti
                    If Not dr("AnnounceEnd") Is DBNull.Value Then
                        .AnnounceEnd = CDate(dr("AnnounceEnd"))
                    End If

                    If Not dr("AnnounceStart") Is DBNull.Value Then
                        .AnnounceStart = CDate(dr("AnnounceStart"))
                    End If
                    .Content.AuthorId = CInt(dr("AuthorId"))
                    .Content.AuthorName = dr("AuthorName").ToString
                    .Content.Body = dr("Body").ToString
                    .Content.ContentId = CInt(dr("ContentId"))
                    .Content.DateCreated = CDate(dr("DateCreated"))
                    .Content.DateUpdated = CDate(dr("DateUpdated"))
                    .Content.IsDeleted = CBool(dr("IsDeleted"))
                    .Content.Subject = dr("Subject").ToString
                    .Content.Summary = dr("Summary").ToString
                    .ContentId = CInt(dr("ContentId"))
                    .Author.AuthorId = .Content.AuthorId
                    .Author.DisplayName = dr("DisplayName").ToString
                    .Author.Email = dr("Email").ToString
                    .Author.FirstName = dr("FirstName").ToString
                    .Author.LastName = dr("LastName").ToString
                    .Author.Username = dr("Username").ToString
                    .IsAnnounce = CBool(dr("IsAnnounce"))
                    .IsApproved = CBool(dr("IsApproved"))
                    .IsArchived = CBool(dr("IsArchived"))
                    .IsDeleted = CBool(dr("IsDeleted"))
                    .IsLocked = CBool(dr("IsLocked"))
                    .IsPinned = CBool(dr("IsPinned"))
                    .ReplyCount = CInt(dr("ReplyCount"))
                    .StatusId = CInt(dr("StatusId"))
                    .TopicIcon = CStr(dr("TopicIcon"))
                    .TopicId = CInt(dr("TopicId"))
                    .TopicType = CType(dr("TopicType"), TopicTypes)
                    .ViewCount = CInt(dr("ViewCount"))
                    .Tags = dr("Tags").ToString
                    .Priority = CInt(dr("Priority").ToString)
                    .Categories = dr("Categories").ToString
                    .ForumURL = dr("ForumURL").ToString
                    .TopicUrl = dr("TopicURL").ToString
                    '.URL = dr("URL").ToString
                    Try
                        .TopicData = dr("TopicData").ToString
                    Catch ex As Exception
                        .TopicData = String.Empty
                    End Try

                End With
            End While
            'If WithSecurity Then
            '    dr.NextResult()
            '    'Dim tmpDr As IDataReader = dr
            '    ti.Security = CType(DotNetNuke.Common.Utilities.CBO.FillObject(dr, GetType(PermissionInfo)), PermissionInfo)
            'End If
            If Not dr.IsClosed Then
                dr.Close()
            End If

            Return ti
        End Function
        Public Sub Topics_Delete(PortalId As Integer, ModuleId As Integer, ByVal ForumId As Integer, ByVal TopicId As Integer, ByVal DelBehavior As Integer)

            DataProvider.Instance().Topics_Delete(ForumId, TopicId, DelBehavior)
            Dim cachekey As String = String.Format("AF-FV-{0}-{1}", PortalId, ModuleId)
            DataCache.CacheClearPrefix(cachekey)
            Try
                Dim objectKey As String = String.Format("{0}:{1}", ForumId.ToString(), TopicId.ToString())
                JournalController.Instance.DeleteJournalItemByKey(PortalId, objectKey)
            Catch ex As Exception

            End Try
            

        End Sub
        Public Sub Topics_Move(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal ForumId As Integer, ByVal TopicId As Integer)
            Dim settings As SettingsInfo = DataCache.MainSettings(ModuleId)
            If settings.URLRewriteEnabled Then
                Try
                    Dim db As New Data.ForumsDB
                    Dim oldForumId As Integer = -1
                    oldForumId = db.Forum_GetByTopicId(TopicId)
                    Dim fc As New ForumController
                    Dim fi As Forum = fc.Forums_Get(oldForumId, -1, False)
                    If Not String.IsNullOrEmpty(fi.PrefixURL) Then
                        Dim dbC As New Data.Common
                        Dim sURL As String = dbC.GetUrl(ModuleId, fi.ForumGroupId, oldForumId, TopicId, -1, -1)
                        If Not String.IsNullOrEmpty(sURL) Then
                            dbC.ArchiveURL(PortalId, fi.ForumGroupId, ForumId, TopicId, sURL)
                        End If
                    End If
                Catch ex As Exception

                End Try
            End If
            DataProvider.Instance().Topics_Move(PortalId, ModuleId, ForumId, TopicId)
        End Sub
        Public Function GetSearchItems(ByVal ModInfo As DotNetNuke.Entities.Modules.ModuleInfo) As DotNetNuke.Services.Search.SearchItemInfoCollection Implements DotNetNuke.Entities.Modules.ISearchable.GetSearchItems
            Dim SearchItemCollection As New DotNetNuke.Services.Search.SearchItemInfoCollection
            Dim dr As IDataReader = Nothing
            Try
                dr = DataProvider.Instance.Search_DotNetNuke(ModInfo.ModuleID)
                Dim SearchItem As DotNetNuke.Services.Search.SearchItemInfo = Nothing
                While dr.Read
                    Dim subject As String = dr("Subject").ToString
                    Dim description As String = String.Empty
                    Dim body As String = dr("Body").ToString
                    Dim authorid As Integer = CInt(dr("AuthorId"))
                    Dim datecreated As Date = CDate(dr("DateCreated"))
                    Dim dateupdated As Date = CDate(dr("DateUpdated"))
                    Dim contentid As Integer = CInt(dr("ContentId"))
                    Dim forumid As Integer = CInt(dr("ForumId"))
                    Dim topicid As Integer = CInt(dr("TopicId"))
                    Dim replyId As Integer = CInt(dr("ReplyId"))
                    Dim jumpid As Integer = topicid
                    If replyId > 0 Then
                        jumpid = replyId
                    End If
                    body = System.Web.HttpUtility.HtmlDecode(body)
                    body = Utilities.StripHTMLTag(body)
                    If Not String.IsNullOrEmpty(body) Then
                        If body.Length > 100 Then
                            description = body.Substring(0, 100) & "..."
                        Else
                            description = body
                        End If
                    End If
                    SearchItem = New DotNetNuke.Services.Search.SearchItemInfo(subject, description, authorid, datecreated, ModInfo.ModuleID, contentid.ToString & "-" & forumid.ToString, body, ParamKeys.ForumId & "=" & forumid & "&" & ParamKeys.ViewType & "=" & Views.Topic & "&" & ParamKeys.TopicId & "=" & topicid & "&" & ParamKeys.ContentJumpId & "=" & jumpid)
                    SearchItemCollection.Add(SearchItem)
                End While
                dr.Close()
                Return SearchItemCollection
            Catch ex As Exception
                Return Nothing
            Finally
                If Not dr Is Nothing Then
                    If Not dr.IsClosed Then
                        dr.Close()
                    End If
                End If
            End Try
        End Function
        Public Function ApproveTopic(PortalId As Integer, TabId As Integer, ModuleId As Integer, ForumId As Integer, TopicId As Integer) As TopicInfo
            Dim ms As SettingsInfo = DataCache.MainSettings(ModuleId)
            Dim fc As New ForumController
            Dim fi As Forum = fc.Forums_Get(ForumId, -1, False, True)

            Dim tc As New TopicsController
            Dim topic As TopicInfo = tc.Topics_Get(PortalId, ModuleId, TopicId, ForumId, -1, False)
            If topic Is Nothing Then
                Return Nothing
            End If
            topic.IsApproved = True
            tc.TopicSave(PortalId, topic)
            tc.Topics_SaveToForum(ForumId, TopicId, PortalId, ModuleId)

            If fi.ModApproveTemplateId > 0 And topic.Author.AuthorId > 0 Then
                Dim oEmail As New Email
                oEmail.SendEmail(fi.ModApproveTemplateId, PortalId, ModuleId, TabId, ForumId, TopicId, 0, String.Empty, topic.Author)
            End If

            Subscriptions.SendSubscriptions(PortalId, ModuleId, TabId, ForumId, TopicId, 0, topic.Content.AuthorId)

            Try
                Dim ctlUtils As New ControlUtils
                Dim sUrl As String = ctlUtils.BuildUrl(TabId, ModuleId, fi.ForumGroup.PrefixURL, fi.PrefixURL, fi.ForumGroupId, fi.ForumID, TopicId, topic.TopicUrl, -1, -1, String.Empty, 1, fi.SocialGroupId)
                Dim amas As New Social
                amas.AddTopicToJournal(PortalId, ModuleId, ForumId, TopicId, topic.Author.AuthorId, sUrl, topic.Content.Subject, String.Empty, topic.Content.Body, fi.ActiveSocialSecurityOption, fi.Security.Read, fi.SocialGroupId)
            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex)
            End Try
            Return topic
        End Function
        'Public Function ActiveForums_GetPostsForSearch(ByVal ModuleID As Integer) As ArrayList
        '    Return CBO.FillCollection(DataProvider.Instance().ActiveForums_GetPostsForSearch(ModuleID), GetType(PostInfo))
        'End Function
    End Class

#End Region
End Namespace

